﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using WPFTrees.Properties;
using System.Windows.Media;
using System.Windows.Controls;

namespace WPFTrees.Workspace
{
    /// <summary>
    /// Class responsible for loading and saving user workspaces.
    /// </summary>
    public static class WorkspaceManager
    {
        private static Color GetColorFromHexa(string hexaColor)
        {
            return Color.FromArgb(
                    Convert.ToByte(hexaColor.Substring(1, 2), 16),
                    Convert.ToByte(hexaColor.Substring(3, 2), 16),
                    Convert.ToByte(hexaColor.Substring(5, 2), 16),
                    Convert.ToByte(hexaColor.Substring(7, 2), 16));
        }

        /// <summary>
        /// Loads workspace.
        /// </summary>
        /// <param name="filename"></param>
        public static void Load(string filename)
        { 
            // TODO: precist vsechny vlastnosti            
            XDocument doc = XDocument.Load(filename);
            var tree = from node in doc.Descendants("workspace")
                       select new
                           {
                               X = node.Element("tree").Attribute("x"),
                               Y = node.Element("tree").Attribute("y"),
                               BackgroundColor = node.Element("settings").Element("backgroundColor").Value,
                               LeafColor = node.Element("settings").Element("leafColor").Value,
                               LeafLabelColor = node.Element("settings").Element("leafLabelColor").Value                               
                           };

            // there is only one item
            foreach (var item in tree)            
            {
                // TODO: nastavid vsechny vlastnosti

                //  background color
                Workspace.Instance.BackgroundColor = GetColorFromHexa(item.BackgroundColor);
                DefaultSettings.Default.BackgroundColor = new SolidColorBrush(Workspace.Instance.BackgroundColor);
                // tree position
                MainWindow window = System.Windows.Application.Current.MainWindow as MainWindow;
                Canvas.SetLeft(window.dockPanel, (double)item.X);
                Canvas.SetTop(window.dockPanel, (double)item.Y);
                // leaf color
                Workspace.Instance.LeafColor = GetColorFromHexa(item.LeafColor);
                DefaultSettings.Default.LeafColor = new SolidColorBrush(Workspace.Instance.LeafColor);
                // leaf label
                Workspace.Instance.LeafLabelColor = GetColorFromHexa(item.LeafLabelColor);
                DefaultSettings.Default.LeafLabelColor = new SolidColorBrush(Workspace.Instance.LeafLabelColor);
            }
            
        }

        /// <summary>
        /// Saves workspace.
        /// </summary>
        /// <param name="filename"></param>
        public static void Save(string filename)
        {             
            XDocument doc = Create();
            doc.Save(filename);
        }

        static XElement[] CreateGroups()
        { 
            //TODO: vytvorit xml elementy pro skupiny
            XElement[] groups = new XElement[0];
            return groups;
        }

        static XElement[] CreateInsertedObjects()
        {
            //TODO: vytvorit xml elementy pro vlozene objekty
            XElement[] objects = new XElement[0];
            return objects;
        }

        static XDocument Create()
        {
            // TODO nastavit vlastnosti
            
            MainWindow window = System.Windows.Application.Current.MainWindow as MainWindow;
            double x = Canvas.GetLeft(window.dockPanel);
            double y = Canvas.GetTop(window.dockPanel);
            double width = window.dockPanel.Width;
            double height = window.dockPanel.Height;

            XDocument doc = new XDocument(
                new XElement("workspace",
                    new XElement("tree",
                        new XAttribute("x",x),
                        new XAttribute("y",y),
                        new XAttribute("width",width),
                        new XAttribute("height",height)
                        ),
                    new XElement("groups",
                            CreateGroups()),
                    new XElement("insertedObjects",
                            CreateInsertedObjects()),
                    new XElement("settings",
                        new XElement("backgroundColor", DefaultSettings.Default.BackgroundColor),
                        new XElement("leafColor", DefaultSettings.Default.LeafColor),
                        new XElement("leafLabelColor", DefaultSettings.Default.LeafLabelColor)
                        )
                    )
                );
            return doc;
        }
    }
}
